'''
Created on Mar 23, 2013

@author: Petko
'''
import re
from uuid import UUID

GROUP_PERMISSIONS = ['udat', 'udat_adm', 'bopin_c', 'bopin_d', 'bopin_r', 'group_adm', 'view_adm'] 

OPINION_PERMISSIONS = ['r', 'c', 'd']

VIEW_PERMISSIONS = ['l', 'm']

opinionScopeRegex = re.compile(r"^([0-9a-fA-F\-]+):([rcd]+)$")

allOpinionScopeRegex = re.compile(r"^all:([rc]+)$")

viewScopeRegex = re.compile(r"^([a-zA-Z0-9_\+\=\/\.\,\\\!\@\#\$\%\^\&\*\(\)]+):([ml]+)$")

allViewScopeRegex = re.compile(r"^all:([ml]+)$")

class Scope(object):
    '''
    classdocs
    '''

    def __init__(self, strScope):
        '''
        Constructor
        '''
        self.data = str(strScope)
        self.isGroupScope = GROUP_PERMISSIONS.count(strScope)
        
        self.isOpinionScope = False
        
        self.isDefaultOpScope = False
        
        self.isViewScope = False
        
        self.isDefaultViewScope = False
        
        if not self.isGroupScope:
            m = opinionScopeRegex.match(strScope)
            if m:
                self.isOpinionScope = True
                self.opinionId = m.group(1)
                
                tmpUUID = UUID(self.opinionId)
                self.opinionBinaryId = tmpUUID.bytes
                
                permissionsStr = str(m.group(2))
                self.opinionPermissions = set(permissionsStr)
            else:
                m = allOpinionScopeRegex.match(strScope)
                if m:
                    self.isOpinionScope = True
                    self.isDefaultOpScope = True
                    permissionsStr = str(m.group(1))
                    self.opinionPermissions = set(permissionsStr)
                else:
                    m = allViewScopeRegex.match(strScope)
                    if m:
                        self.isViewScope = True
                        self.isDefaultViewScope = True
                        permissionsStr = str(m.group(1))
                        self.viewPermissions = set(permissionsStr)
                    else:
                        m = viewScopeRegex.match(strScope)
                        if m:
                            self.isViewScope = True
                            self.viewId = m.group(1)
                            
                            permissionsStr = str(m.group(2))
                            self.viewPermissions = set(permissionsStr)
                        
    def __eq__(self, obj):
        if isinstance(obj, Scope):
            return self.data.__eq__(obj.data)
        elif isinstance(obj, basestring):
            return self.data.__eq__(obj)
        else:
            return object.__eq__(self, obj)
    
    def __cmp__(self, other):
        if isinstance(other, Scope):
            return self.data.__cmp__(other.data)
        elif isinstance(other, basestring):
            return self.data.__cmp__(other)
        else:
            return object.__cmp__(self, other)
    
    def __ne__(self, obj):
        return not self.__eq__(obj)
    
    def __hash__(self):
        return self.data.__hash__()
    
    def __repr__(self):
        return self.data
    
    def __str__(self):
        return str(self.data)
    
def parseScopeList(scopeListStr):
    scopeList = scopeListStr.split(' ')
    result = set()
    for scopeStr in scopeList:
        result.add(Scope(scopeStr))
    return result
